/**
 * @ author: 汪孟苹
 */
define(["jquery","template","contextmenu","dg/datagride.common","bbcare","fu/app/comm/comm","dg/datagride.attention"],
	function($,template,$contextmenu,$common,$bb,$comm,$attention){
	var quest = {};
	var URL = $bb.baseUrl("followup");

	/**
	 * 数据处理
	 */
	quest.formatData = function(newObj){
    	if( newObj.list && typeof newObj.list != "string" ){
			newObj.list = JSON.stringify(newObj.list);
		}
		return newObj;
	}

	/**
	 * tpl模板
	 */
	var tpl_label = function(){
		return  '<script id="tpl_right_click_label" type="text/html">'+
				'	<div style="{{label.style}}">{{label.value}}</div>'+
				'</script>';
	}
	var tpl_input = function(){
		return  '<script id="tpl_right_click_input" type="text/html">'+
				'	<div class="datagride-rightClick validatebox" style="{{style}}" data-questionId="{{questionId}}" data-recordId="{{recordId}}" data-list="{{list}}" data-attentionType="{{attentionType}}" data-attention="{{attention}}">{{value}}</div>'+
				'</script>';
	}
	quest.tpl = function(){
		var tpl = {};
		tpl.label = tpl_label;
		tpl.input = tpl_input;
		return tpl;
	}

	/**
	 * 右击事件相关方法
	 */
	// 禁止浏览器自带的默认右击
	var bannedDefaultRightClick = function(obj1,obj2){
		obj1.unbind("contextmenu");
		obj2.bind("contextmenu",function(e){ 
			e.preventDefault();
			e.stopPropagation();
		});
	}

	// 右击面板模板
	var tpl_contextmenu = function(){
		return  '<script id="tpl_contextmenu" type="text/html">'+
				'	<ul class="dropdown-menu" role="menu">'+
				'		{{each list as value i}}'+
		        '			<li data-url="{{value.url}}" data-key="{{value.key}}"><a tabindex="-1" href="javascript:void(0);">{{value.text}}</a></li>'+
		        '		{{/each}}'+
		        '	</ul>'+
		        '</script>';
	}
	// 右击面板初始化
	var contextmenuInit = function(options){
		$("#contextmenu").html( tpl_contextmenu() );
		var html = template("tpl_contextmenu",options);
		$("#contextmenu").html(html);
	}

	// 批量修改模态框模板
	var tpl_batchModify = function(){
		return  '<script id="tpl_batchModify" type="text/html">'+
				'	<div class="form-group">'+
				'		<input type="text" class="form-control batchModify-input">'+
				'	</div>'+
				'	<div class="text-right">'+
				'		<button type="button" class="btn btn-sm btn-default batchModify-confirm">确定</button>'+
				'		<button type="button" class="btn btn-sm btn-primary batchModify-cancel">取消</button>'+
				'	</div>'+
		        '</script>';
	}
	// 批量修改模态框初始化
	var batchModifyInit = function(options){
		$("." + options.className + " .modal-body").html( tpl_batchModify() );
		var html = template("tpl_batchModify",options);
		$("." + options.className + " .modal-body").html(html);
	}
	// 修改记录模态框模板
	var tpl_recordModify = function(){
		return  '<script id="tpl_recordModify" type="text/html">'+
				'	<div class="table-responsive">'+
				'		<table class="table table-bordered table-condensed table-hover">'+
				'			<tr>'+
				'				<td>修改时间</td>'+
				'				<td>修改剂量</td>'+
				'				<td>修改原因</td>'+
				'			</tr>'+
				'			<tr>'+
				'				<td>2017-12-29</td>'+
				'				<td>44</td>'+
				'				<td>胡医生修改药品未用量</td>'+
				'			</tr>'+
				'		</table>'+
				'	</div>'+
		        '</script>';
	}
	// 修改记录模态框初始化
	var recordModifyInit = function(options){
		$("." + options.className + " .modal-body").html( tpl_recordModify() );
		var html = template("tpl_recordModify",options);
		$("." + options.className + " .modal-body").html(html);
	}




	// 请求接口成功后的回调
	var ajaxInterfaceCallBack = function(data,dom){
		console.log(data);
		//dom.html(data.xxx);
	}
	// 请求接口
	var ajaxInterface = function(url,params,dom,callback){
		$bb.ajax({
			url: URL + url,
			data:JSON.stringify({
				data : params
			}),
			success: function(data){
				if(data.success){ 
					callback(data.data,dom);
				}else{
					$bb.toastShow(data.msg ? data.msg : "请求接口出错！");
				}
			}
		});
	}





	// 单项修改、多项修改
	var singleModify = function(key,obj){
		if( obj.find("input").length == 0 ){
			var oldText = obj.text();
			var width = obj.width();
			var h = '<input type="text" class="form-control" style="width:'+ width +'px">';
			obj.html(h);
			if(key == "01") {
				obj.find("input").focus().val(oldText);
			}else{
				obj.find("input").val(oldText);
			}
		}else{
			var oldText = obj.find("input").val();
		}
		obj.find("input").unbind("blur");
		obj.find("input").blur(function(){
			obj.removeClass("active");
			var newText = $(this).val();
			if( newText == oldText ){
				obj.html( oldText );
			}else{
				/*var questionId = obj.attr("data-questionId");
				var recordId = obj.attr("data-recordId");
				var params = {
					"value":newText,
					"questionId":questionId,
					"recordId":recordId
				};
				ajaxInterface(url,params,obj,ajaxInterfaceCallBack);*/
				obj.html( newText ); // 等有了接口这里需要删除，暂时未了展示
				$attention.onChange( obj );
			}
		});
	}
	// 批量修改
	var batchModify = function($dom){
		var className = "batchModifyModal";
		$comm.modal({
			"className": className,
			"size": "modal-sm",
            "tit": "请输入批量修改值",
		},function(){
	        batchModifyInit({ "className":className });
	        $("." + className + " .modal-body .batchModify-input").focus();
	        $("." + className).modal('show');
	        // 确认
	        $("." + className + " .batchModify-confirm").unbind("click");
	        $("." + className + " .batchModify-confirm").click(function(){
	        	var value = $(this).parents(".modal-body").find(".batchModify-input").val();
	        	// 有接口后需要改为接口(现在只是展示一下)
	        	$dom.each(function(i){
					$dom.eq(i).html(value);
					$attention.onChange( $dom.eq(i) );
					$dom.eq(i).removeClass("active");
				});
				$("." + className).modal('hide');
	        });
	        // 取消
	        $("." + className + " .batchModify-cancel").unbind("click");
	        $("." + className + " .batchModify-cancel").click(function(){
	        	$("." + className).modal('hide');
	        });
        });
	}
	// 修改记录
	var recordModify = function(obj){
		// 此处应先请求数据
		var className = "recordModifyModal";
		$comm.modal({
			"className": className,
            "tit": "修改记录",
		},function(){
	        recordModifyInit({ 
	        	"className":className,
	        	"list":""
	        });
	        $("." + className).modal('show');
        });
	}
	// 交换
	var exchange = function(obj){
		//obj.parent("td").addClass("exchange");
		//console.log(obj);
	}
	// 对右击面板进行的一系列操作(单项修改、多项修改、批量修改、)
	var onOperation = function(){
		var key = $(this).attr("data-key");
		//var url = $(this).attr("data-url");
		switch( key ){
			case "00": // 用药记录
				break;
			case "01": // 单项修改
				var oDiv = $(this).data("oDiv");
				singleModify(key,oDiv);
				break;
			case "02": // 多项修改
				var $dom = $(this).data("dom");
				$dom.each(function(i){
					singleModify(key,$dom.eq(i));
				});
				break;
			case "03": // 批量修改
				var $dom = $(this).data("dom");
				batchModify($dom);
				break;
			case "04": // 交换
				var oDiv = $(this).data("oDiv");
				exchange(oDiv);
				break;
			case "05": // 修改记录
				var oDiv = $(this).data("oDiv");
				recordModify(oDiv);
				break;
		}
	}

	/**
	 * 事件
	 */
	quest.event = function(params){
		var $domAll = $(".datagride-rightClick");
		bannedDefaultRightClick( $domAll,$domAll.not(".active") );
		$domAll.unbind("click");
		$domAll.click(function(){
			// 选中
			if( $(this).hasClass("active") ){
				$(this).removeClass("active").removeAttr("data-toggle");
			}else{
				$(this).addClass("active").attr("data-toggle","context");
			}
			var $domActive = $(".datagride-rightClick.active");
			var $domNoActive = $domAll.not(".active");
			// 没选中的禁掉右击
			bannedDefaultRightClick( $domAll,$domNoActive );
			// 重新绑定
			$domAll.off("contextmenu.context.data-api");
			$domActive.contextmenu({
	        	target:'#contextmenu',
				before: function(e,context) {
					var oDiv = $(e.currentTarget);
					var $dom = $(e.currentTarget).parents(".datagride").find(".datagride-rightClick.active");
					if( oDiv.attr("data-list") ){
						var list = JSON.parse( oDiv.attr("data-list") );
						contextmenuInit({
							"list":list
						});
						$("#contextmenu ul.dropdown-menu li").unbind("click");
						$("#contextmenu ul.dropdown-menu li").data({"oDiv":oDiv,"dom":$dom});
						$("#contextmenu ul.dropdown-menu li").click(onOperation);
					}
				},
				onItem: function(context,e){}
	        });
		});
	}

	return quest;
});